#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QToolBar>
#include <QTimer>
#include <QDockWidget>
#include <QDialog>
#include <QMessageBox>
#include <QColorDialog>
#include <QFileDialog>
#include <QFontDialog>
#include <QInputDialog>
#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //设置标题
    this->setWindowTitle("test");
    //创建菜单栏--防止内存泄露
    QMenuBar* menubar=this->menuBar();
    //将菜单栏关联到窗口
    this->setMenuBar(menubar);
    //创建菜单
    QMenu *menufile=new QMenu("文件(&F)");
    QMenu *menuedit=new QMenu("编辑(&E)");
    QMenu *menuhelp=new QMenu("帮助(&H)");
    //添加图标
    //menufile->setIcon(QIcon(":/image/file.jpg"));
    //menuedit->setIcon(QIcon(":/image/edit.jpg"));
    //menuhelp->setIcon(QIcon(":/image/file.jpg"));
    //添加菜单
    menubar->addMenu(menufile);
    menubar->addMenu(menuedit);
    menubar->addMenu(menuhelp);
    //创建菜单项
    QAction *actionopen=new QAction("open(&O)");
    QAction *actionclose=new QAction("close(&C)");
    //添加图标
    actionopen->setIcon(QIcon(":/image/open.jpg"));
    actionclose->setIcon(QIcon(":/image/close.jpg"));
    //添加菜单项
    menufile->addAction(actionopen);
    menufile->addSeparator();
    menufile->addAction(actionclose);
    //给菜单项添加动作
    connect(actionopen,&QAction::triggered,this,&MainWindow::file_open);
    connect(actionclose,&QAction::triggered,this,&MainWindow::file_close);
    //创建工具栏
    QToolBar* toolopen=new QToolBar(this);
    QToolBar* toolclose=new QToolBar(this);
    //添加菜单项到工具栏
    toolopen->addAction(actionopen);
    toolclose->addAction(actionclose);
    //添加工具栏到窗口
    this->addToolBar(Qt::LeftToolBarArea,toolopen);
    this->addToolBar(Qt::TopToolBarArea,toolclose);
    //    //只允许停靠在左侧
    //    toolopen->setAllowedAreas(Qt::LeftToolBarArea);
    //    //禁止浮动
    //    toolclose->setFloatable(false);
    //    //禁止移动
    //    toolclose->setMovable(false);

    //创建状态栏--防止内存泄露
    QStatusBar*statusbar=this->statusBar();
    //添加状态栏
    setStatusBar(statusbar);
    //往状态栏中添加标签
    labelcursor=new QLabel(this);
    //将标签添加到状态栏中
    statusbar->addWidget(labelcursor);
    //创建一个时钟
    setMouseTracking(true);  // 启用鼠标跟踪
    QTimer *timer =new QTimer(this);
    connect(timer,&QTimer::timeout,this,&MainWindow::get_cursorPoint);
    timer->start(100);
    //创建浮动窗口
    QDockWidget*dockwidget=new QDockWidget("浮动窗口",this);
    addDockWidget(Qt::BottomDockWidgetArea,dockwidget);
    //浮动窗口添加标题
    //dockwidget->setWindowTitle("1111");
    //创建子窗口，并添加到浮动窗口----》因为浮动窗口只能添加一个控件
    QWidget* container=new QWidget();
    dockwidget->setWidget(container);
    //添加一些控件
    QVBoxLayout* layout=new QVBoxLayout();
    container->setLayout(layout);

    QLabel* floutLabel=new QLabel("******");
    QPushButton* floutButton=new QPushButton("********");
    layout->addWidget(floutLabel);
    layout->addWidget(floutButton);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::file_open()
{
    ui->label->setText("open");
}

void MainWindow::file_close()
{
    this->close();
}

void MainWindow::get_cursorPoint()
{
    QPoint pos = QCursor::pos();
    int x=pos.x();
    int y=pos.y();
    QString str="x:"+QString::number(x)+"y:"+QString::number(y);
    labelcursor->setText(str);
}


void MainWindow::on_pushButton_exec_clicked()
{
    QDialog dialog(this);
    dialog.resize(200,100);
    dialog.exec();
}

void MainWindow::on_pushButton_show_clicked()
{
    QDialog *dialog=new QDialog(this);
    dialog->resize(200,100);
    //自动析构-释放堆上空间
    dialog->setAttribute(Qt::WA_DeleteOnClose);
    dialog->show();
}

void MainWindow::on_pushButton_message_clicked()
{
    QMessageBox *message =new QMessageBox(this);
    message->setWindowTitle("消息框");
    message->setText("这个是QMessageBox");
    message->setIcon(QMessageBox::Icon::Critical);
    message->setStandardButtons(QMessageBox::Ok|QMessageBox::Cancel);
    message->setAttribute(Qt::WA_DeleteOnClose);
    //message->show();

    int result=message->exec();
    if(result==QMessageBox::Ok)
    {
        ui->label->setText("QMessageBox::critical::OK");
        return;
    }
    if (result==QMessageBox::Cancel)
    {
        ui->label->setText("QMessageBox::critical::Cancel");
        return;
    }
}

void MainWindow::on_pushButton_message_2_clicked()
{
    int result= QMessageBox::warning(this,"对话框标题","对话框文本",QMessageBox::Ok|QMessageBox::Cancel);
    if(result==QMessageBox::Ok)
    {
        ui->label->setText("QMessageBox::warning::OK");
        return;
    }
    if (result==QMessageBox::Cancel)
    {
        ui->label->setText("QMessageBox::warning::Cancel");
        return;
    }
}

void MainWindow::on_pushButton_color_clicked()
{
    QColorDialog colordialog(this);
    QColor color=colordialog.exec();
    qDebug()<<color;
    QString str ="r:"+QString::number(color.red());
    str +="g:"+QString::number(color.green());
    str +="b:"+QString::number(color.blue());
    ui->label->setText(str);
}

void MainWindow::on_pushButton_color_2_clicked()
{
    QColor color= QColorDialog::getColor(QColor(0,0,0),this,"选着颜色");
    qDebug()<<color;
    QString str ="r:"+QString::number(color.red());
    str +="g:"+QString::number(color.green());
    str +="b:"+QString::number(color.blue());
    ui->label->setText(str);
}

void MainWindow::on_pushButton_file_clicked()
{
    QFileDialog *filedialog = new QFileDialog(this);
    filedialog->setWindowTitle("文件窗口");
    ui->label->setText("pathname:需要重写");
}

void MainWindow::on_pushButton_file_2_clicked()
{
    QString pathname=QFileDialog::getOpenFileName(this);
    ui->label->setText(pathname);
}

void MainWindow::on_pushButton_font_clicked()
{
    bool ok=false;
    QFont font=QFontDialog::getFont(&ok);
    ui->label->setText("font:需要重写");
}

void MainWindow::on_pushButton_font_2_clicked()
{
    bool ok=false;
    QFont font=QFontDialog::getFont(&ok);
    qDebug()<<"ok=:"<<ok<<"font:="<<font;
}

void MainWindow::on_pushButton_input_clicked()
{
    int result=QInputDialog::getInt(this,"整数获取","输入整数=:");
    ui->label->setText(QString::number(result));

}

void MainWindow::on_pushButton_input_2_clicked()
{
    double result=QInputDialog::getDouble(this,"浮点数获取","输入浮点数=:");
    qDebug()<<result;
}

void MainWindow::on_pushButton_input_3_clicked()
{
    QStringList items;
    items.push_back("aaaa");
    items.push_back("bbbb");
    items.push_back("cccc");
    items.push_back("dddd");
    QString str=QInputDialog::getItem(this,"获取string","输入string=:",items);
    ui->label->setText(str);
}
